package edu.hrbu.trade01.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.hrbu.trade01.annotation.MyLog;
import edu.hrbu.trade01.entity.Menu;
import edu.hrbu.trade01.service.MenuService;
import edu.hrbu.trade01.service.RoleMenuService;
import edu.hrbu.trade01.service.RoleService;
import edu.hrbu.trade01.util.MenuUtil;
import edu.hrbu.trade01.util.R;
import edu.hrbu.trade01.util.TableR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static edu.hrbu.trade01.util.ConstUtil.*;

/**
 * @author 徐登宇
 */
@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

//    // 菜单条件分页查询
//    @RequestMapping("/list")
//    public TableR list(@RequestParam(value = "page", defaultValue = "1") int page,
//                  @RequestParam(value = "limit", defaultValue = "5") int limit,
//                  @RequestParam(value = "name", required = false) String name) {
//
//        Page<Menu> pageModel = menuService.getPageWithCondition(page, limit, name);
//
////        log.info("@"+pageModel);
//        return TableR.ok(pageModel.getTotal(), pageModel.getRecords());
////        return R.ok();
//    }

    // 获取所有的菜单，以及自己拥有的菜单
    @RequestMapping("/pre/assign")
    public R preAssign(@RequestParam("roleId") Long roleId) {
        // 获取所有的菜单，以及自己拥有的菜单
        Map<String, List<Menu>> map = menuService.getMenuMapByRoleId(roleId);
        log.info("---" + map.get("allMenuList"));
        return R.ok(map);
    }

    @MyLog(title = "分配菜单", businessType = LOG_TYPE_SYS)
    // 重新分配菜单
    @PostMapping("/to/assign")
    public R toAssign(@RequestParam("ids") List<Long> menuIdList, @RequestParam("roleId") Long roleId) {
        roleMenuService.toAssign(menuIdList, roleId);
        return R.ok();
    }

    // 树形列表
    @RequestMapping("/list")
    public R list() {
        List<Menu> list = menuService.list();
        ArrayList<Menu> menuTree = MenuUtil.buildTree(list, 1);
        return R.ok(menuTree);
    }

    @MyLog(title = LOG_TITLE_UPDATE, businessType = LOG_TYPE_SYS)
    @PostMapping("/update")
    public R update(@RequestBody Menu menu) {
        if (ObjectUtils.isEmpty(menu.getPid())) menu.setPid(0L);
        menuService.updateById(menu);
        log.info("menu::" + menu);
        return R.ok();
    }

    @MyLog(title = LOG_TITLE_DEL, businessType = LOG_TYPE_SYS)
    @PostMapping("/remove")
    public R remove(@RequestParam("id") Long id) {
        menuService.removeById(id);
        return R.ok();
    }

    @MyLog(title = LOG_TITLE_SAVE, businessType = LOG_TYPE_SYS)
    @PostMapping("/save")
    public R save(@RequestBody Menu menu) {
        menuService.save(menu);
        return R.ok();
    }

}
